/*
 * 本地服务器
 * @author li.xiaoxi
 * @date 2016-12-15
 */
//依赖模块
var fs = require('fs');

//基于nodejs平台的web开发框架
var express = require('express');
var app = express();
var webpack = require('webpack')
var router = require('../middleware/router');
var webpackMiddleware = require('../middleware/webpack')
var ip = require('../util/ip')

//变量
var port = global.think_config.port;

module.exports = function(){
  //引入webpack的配置文件和生成webpack的编译器
  var webpackConfig = require('../util/webpack/dev.js')
  var config = webpackConfig(),
      compiler = webpack(config);

  //webpack 本地开发环境
  var devMiddleware = webpackMiddleware.dev(config, compiler), //将编译器挂载给webpack-dev-middleware
      hotMiddleware = webpackMiddleware.hot(config, compiler); //将编译器挂载给webpack-hot-middleware
  app.use(devMiddleware);
  app.use(hotMiddleware);

  //设置虚拟目录 express.static是唯一一个静态中间件
  //root: 是静态资源文件所在的根目录
  app.use(express.static(process.cwd(), {
    //directory: true
  }));

  //在应用中加载路由模块
  app.use(router);

  //启动本地服务器
  app.listen(port, function(err){
    if (err) {
      console.log('【error】:')
      console.log(err)
      return
    }
    console.log('Listening at http://localhost:' +port)
    console.log('Listening at http://'+ip().extra+':' + port + '\n')
  });

}
